package com.ftg.learn.day12.datastruct;

/**
 * @author 布衣
 */
public class LearnLinked {

    //头节点
    Node head;

    private class Node {
        private int data;
        private Node next;
        private Node prev;

        public Node() {
        }

        public Node(int data) {
            this.data = data;
        }
        public Node(Node prev,int data,Node next) {
            this.data = data;
            this.prev = prev;
            this.next = next;
        }
    }

    public Node getLast(Node current){
        System.out.println(current.data);
        if (current.next == null) {
            return current;
        }
        return getLast(current.next);
    }

    public Node getPrev(Node current){
        System.out.println(current.data);
        if (current.prev == null) {
            return current;
        }
        return getPrev(current.prev);
    }

    public void add(int d) {
        if (head == null) {
            head = new Node(null,d,null);
        }else{
            Node last = getLast(head);
            last.next = new Node(last,d,null);
        }
    }

    public static void main(String[] args) {
        LearnLinked linked = new LearnLinked();
        linked.add(4);
        linked.add(5);
        linked.add(6);
        linked.add(7);
        linked.add(8);
        linked.add(6);


        Node current = linked.getLast(linked.head);
        System.out.println("前取");
        linked.getPrev(current);

        System.out.println("后取");
        linked.getLast(linked.head);

    }

}
